package com.apinji.unionpay.dao.daoimpl;

import com.apinji.common.domain.TransactionRecord;
import com.apinji.common.domain.mapper.TransactionRecordMapper;
import com.apinji.common.domain.wrapper.Reply;
import com.apinji.common.plugin.Mutual;
import com.apinji.common.plugin.StrUtils;
import com.apinji.common.data.PayStatusEnum;
import com.apinji.unionpay.dao.TransactionRecordDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by 轩辕夏夏 on 2017/7/7.
 */
@Service
public class TransactionRecordDaoImpl implements TransactionRecordDao {

    @Autowired
    private TransactionRecordMapper transactionRecordMapper;

    @Override
    public Reply updatePayStatus(String id, int payStatus, String memo) {
        if(StrUtils.isEmpty(id)) {
            return Mutual.invalidParam("主键ID为空");
        }
        //
        TransactionRecord transactionRecord = transactionRecordMapper.getOneById(id);
        if(null == transactionRecord) {
            return Mutual.notExist("ID为："+id+"的交易记录不存在");
        }
        //
        if(payStatus != PayStatusEnum.FAILD_DEAL.getStatus() && payStatus != PayStatusEnum.SUCCESS_DEAL.getStatus()) {
            if(payStatus > transactionRecord.getPayStatus()) {
                transactionRecordMapper.updatePayStatus(id, payStatus, memo);
            }
        } else if(payStatus == PayStatusEnum.SUCCESS_DEAL.getStatus() || payStatus == PayStatusEnum.FAILD_DEAL.getStatus()) {
            transactionRecordMapper.updatePayStatus(id, payStatus, memo);
        } else {
            return Mutual.serverError("状态修改失败");
        }
        //
        return Mutual.success();
    }

}
